package com.ebt.m.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import java.util.List;

/**
 * EbtAccordion 适配器
 *
 * @author Michael.li
 */
public abstract class AccordionAdapter<T> {
    protected List<T> list;
    protected Context context;

    public AccordionAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;
        duration = context.getResources().getInteger(android.R.integer.config_mediumAnimTime);
    }

    /**
     * 手风琴的项目个数
     *
     * @return
     */
    public int getCount() {
        return list.size();
    }

    public T getItem(int position) {
        return list.get(position);
    }

    /**
     * 是否可见
     *
     * @param position
     * @return
     */
    public int getVisibility(int position) {
        return View.VISIBLE;
    }

    /**
     * 获得标题栏的view
     *
     * @param position
     * @param parent
     * @return
     */
    public abstract View getHeaderView(int position, ViewGroup parent);

    /**
     * 获得内容区的view
     *
     * @param position
     * @param parent
     * @return
     */
    public abstract View getContentView(int position, ViewGroup parent);

    /**
     * 是否可以展开
     *
     * @param position
     * @return
     */
    public abstract boolean ifCanExpand(int position);

    /**
     * 展开时显示第2个子视图
     *
     * @param position
     * @param headerView
     */
    public void onExpanded(int position, View headerView) {
        if (!isAnimationRunning) {
            isAnimationRunning = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    isAnimationRunning = false;
                }
            }, duration);
        }
        ViewGroup currentView = (ViewGroup) headerView.getParent();
        final View v = currentView.getChildAt(1);
        v.setVisibility(View.VISIBLE);
        v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();
        v.getLayoutParams().height = 0;
        animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = (interpolatedTime == 1) ? LayoutParams.WRAP_CONTENT : (int) (targetHeight * interpolatedTime);
                v.requestLayout();
            }
            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        animation.setDuration(duration);
        v.startAnimation(animation);
    }

    /**
     * 收缩
     *
     * @param position
     * @param headerView
     */
    public void onCollapsed(int position, View headerView) {
        ViewGroup currentView = (ViewGroup) headerView.getParent();
        currentView.getChildAt(1).setVisibility(View.GONE);
    }

    private Boolean isAnimationRunning = false;
    private Integer duration;
    private Animation animation;
}